package com.xingkeduo.business.user.domain;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.user.service.*;
import com.xingkeduo.pojo.query.CraftsmanSocialQO;
import com.xingkeduo.pojo.query.JobseekerSocialQO;
import com.xingkeduo.pojo.table.*;
import com.xingkeduo.pojo.utils.Base64Utils;
import com.xingkeduo.util.pojo.enums.LeavingTypeEnum;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.BusinessException;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ${craftsman_back}业务
 *
 * @author 高杰
 * @time 2018年12月24日 下午6:57:53
 * @description
 */
@Component
public class AssessmentDomain {

    private static Logger log = LoggerFactory.getLogger(AssessmentDomain.class);
    @Autowired
    private JobseekerService jobseekerService;
    @Autowired
    private JobseekerSocialService jobseekerSocialService;
    @Autowired
    private JobseekerJobService jobseekerJobService;
    @Autowired
    private JobseekerJobDetailService jobseekerJobDetailService;
    @Autowired
    private JobseekerAssessService jobseekerAssessService;
    @Autowired
    private CraftsmanService craftsmanService;
    @Autowired
    private CraftsmanJobService craftsmanJobService;
    @Autowired
    private CraftsmanJobDetailService craftsmanJobDetailService;
    @Autowired
    private CraftsmanSocialService craftsmanSocialService;
    @Autowired
    private CraftsmanLevelService craftsmanLevelService;
    @Autowired
    private CraftsmanDutyService craftsmanDutyService;
    @Autowired
    private CraftsmanTraceService craftsmanTraceService;
    @Autowired
    private CraftsmanLeavingService craftsmanLeavingService;

    /**
     * 注册
     *
     * @param jobseeker
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月26日 上午11:39:56
     */
    @Transactional
    public JobseekerTO regist(JobseekerTO jobseeker) throws Throwable {
        log.info("传过来的信息数据----------" + JSON.toJSONString(jobseeker));

        JobseekerTO jobseekerDb = jobseekerService.selectByLogin(jobseeker.getLogin());
        if (jobseekerDb == null) {
            CraftsmanTO craftsmanTO = craftsmanService.selectByLogin(jobseeker.getLogin());
            if (null != craftsmanTO) {
                log.info("求职者公众号授权,已经是发型师的用户请求");
                throw new BusinessException(BusinessException.Type.DEFAULT, "您已经是发型师.");
            }
            jobseeker.setType(JobseekerTO.Type.HAIRCUT).setLogin(jobseeker.getLogin());
            if (null != jobseeker.getWechatName()) {
                String nickName = Base64Utils.encodeParam(jobseeker.getWechatName());
                log.info("求职者注册 数据名字加密---------" + nickName);
                jobseeker.setWechatName(nickName);
            }
            jobseekerService.insert(jobseeker);
            return jobseeker;
        }
        log.info("求职者信息状态----------" + JSON.toJSONString(jobseekerDb));
        if (jobseekerDb.getStatus() == JobseekerTO.Status.GRADUATION) {
            CraftsmanTO craftsman = craftsmanService.selectByLogin(jobseeker.getLogin());
            if (craftsman == null)
                throw new BusinessException(BusinessException.Type.DEFAULT, "入职信息不存在");
            if (craftsman.getStatus() != CraftsmanTO.Status.LEAVE)
                throw new BusinessException(BusinessException.Type.DEFAULT, "入职信息已存在");
        } else if (jobseekerDb.getStatus() == JobseekerTO.Status.TEST2) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "正在考核中, 不能注册");
//            return jobseekerDb;
        }
//        else if (!(jobseekerDb.getStatus() == JobseekerTO.Status.TEST1ELIMINATE
//                || jobseekerDb.getStatus() == JobseekerTO.Status.TEST1RESERVE
//                || jobseekerDb.getStatus() == JobseekerTO.Status.TEST2ELIMINATE
//                || jobseekerDb.getStatus() == JobseekerTO.Status.TEST2RESERVE )) {
//            throw new BusinessException(BusinessException.Type.DEFAULT, "正在考核中, 不能注册");
//        }
        else if (jobseekerDb.getStatus() == JobseekerTO.Status.INIT || jobseekerDb.getStatus() == JobseekerTO.Status.TEST1) {
            //如果是初试 25 在页面上是 填写信息阶段,放行
            return jobseekerDb;
        } else if (jobseekerDb.getStatus() == JobseekerTO.Status.TEST1ELIMINATE || jobseekerDb.getStatus() == JobseekerTO.Status.TEST2ELIMINATE) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "入职信息已存在");
        }
        jobseeker.setId(jobseekerDb.getId());
        jobseeker.setStatus(JobseekerTO.Status.TEST1);
        log.info("jobseeker------wechatName----" + jobseekerDb.getWechatName());
        if (null != jobseeker.getWechatName()) {
            jobseeker.setWechatName(Base64Utils.encodeParam(jobseeker.getWechatName()));
            log.info("jobseeker------wechatName--Base64--" + jobseeker.getWechatName());
        }
        jobseeker.setUpdatedBy(jobseeker.getId());
        jobseekerService.update(jobseeker);

        CraftsmanTraceTO craftsmanTrace = new CraftsmanTraceTO().setLogin(jobseeker.getLogin())
                .setType(CraftsmanTraceTO.Type.INIT);
        craftsmanTraceService.insert(craftsmanTrace);
        return jobseeker;
    }

    /**
     * 填写身份信息
     *
     * @param jobseekerSocial
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月26日 上午11:40:04
     */
    @Transactional
    public void fillJobseekerSocial(JobseekerSocialTO jobseekerSocial) throws Throwable {
        log.info("求职者身份信息创建------------" + JSON.toJSONString(jobseekerSocial));
        if (jobseekerService.countByPrimary(jobseekerSocial.getId()) == 0)
            throw new BusinessException(BusinessException.Type.DEFAULT, "账户不存在");

        List<CraftsmanSocialTO> socialTOList;
        socialTOList = craftsmanSocialService.selectByQuery(new CraftsmanSocialQO().setIdCard(jobseekerSocial.getIdCard()));
        if (null != socialTOList && socialTOList.size() > 0) {
            log.info("发型师信息表已经存在该发型师的身份证号");
            throw new BusinessException(BusinessException.Type.DEFAULT, "发型师身份证号已存在");
        }

        JobseekerSocialTO jobseekerSocialDb = jobseekerSocialService.selectByPrimary(jobseekerSocial.getId());
        List<Long> list;
        JobseekerSocialQO jobseekerSocialQO = new JobseekerSocialQO();
        jobseekerSocialQO.setIdCard(jobseekerSocial.getIdCard());
        list = jobseekerSocialService.selectPrimaryByQuery(jobseekerSocialQO);//有数据
        if (list != null && list.size() > 0) {
            if (jobseekerSocialDb != null) {
                if (list.get(0) != jobseekerSocialDb.getId()) {
                    log.info("求职者信息表已经存在该发型师的身份证号");
                    throw new BusinessException(BusinessException.Type.DEFAULT, "求职者身份证号已存在");
                }
            } else {
                throw new BusinessException(BusinessException.Type.DEFAULT, "求职者身份证号已存在");
            }
        }

        socialTOList = craftsmanSocialService.selectByQuery(new CraftsmanSocialQO().setPhone(jobseekerSocial.getPhone()));
        if (null != socialTOList && socialTOList.size() > 0) {
            log.info("发型师信息表已经存在该发型师的手机号");
            throw new BusinessException(BusinessException.Type.DEFAULT, "发型师手机号已存在");
        }
        list = jobseekerSocialService.selectPrimaryByQuery(new JobseekerSocialQO().setPhone(jobseekerSocial.getPhone()));
        if (list != null && list.size() > 0) {
            if (jobseekerSocialDb != null) {
                if (list.get(0) != jobseekerSocialDb.getId()) {
                    log.info("求职者信息表已经存在该发型师的手机号");
                    throw new BusinessException(BusinessException.Type.DEFAULT, "求职者手机号已存在");
                }
            } else {
                throw new BusinessException(BusinessException.Type.DEFAULT, "求职者手机号已存在");
            }
        }

        String name = jobseekerSocial.getName();
        jobseekerSocial.setName(name);
        if (jobseekerSocialDb == null) {
            jobseekerSocialService.insert(jobseekerSocial);
        } else {
            jobseekerSocialService.update(jobseekerSocial.setUpdatedBy(jobseekerSocial.getId()));
        }

    }

    /**
     * 填写工作经历
     *
     * @param jobseekerJob
     * @param jobseekerJobDetailList
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年2月28日 下午4:42:55
     */
    @Transactional
    public JobseekerJobTO fillJobseekerJobDetails(JobseekerJobTO job, List<JobseekerJobDetailTO> jobDetails)
            throws Throwable {
        log.info("user  填写工作信息" + job.toString());
        log.info("user  填写工作信息" + jobDetails.toString());
        if (jobseekerService.countByPrimary(job.getId()) == 0)
            throw new BusinessException(BusinessException.Type.DEFAULT, "账户不存在");

        JobseekerJobTO jobDb = jobseekerJobService.selectByPrimary(job.getId());
        if (jobDb == null) {
            job.setCreatedBy(job.getId()).setUpdatedBy(job.getCreatedBy());
            jobseekerJobService.insert(job);
            jobseekerJobDetailService.insert(jobDetails, job);

            return new JobseekerJobTO().setId(0).setCode(0);
        }

        jobDb.setUpdatedBy(jobDb.getId());
        jobseekerJobService.update(jobDb);

        for (Iterator<JobseekerJobDetailTO> iterator = jobDetails.iterator(); iterator.hasNext(); ) {
            JobseekerJobDetailTO jobseekerJobDetail = iterator.next();
            if (jobseekerJobDetail.getId() > 0) {
                if (jobseekerJobDetailService.update(jobseekerJobDetail) < 1)
                    throw new DataException(DataException.Type.FAIL, "工作经历");
                iterator.remove();
            }
        }
        if (jobDetails.size() > 0) {
            //查询没有作废的信息
            List<JobseekerJobDetailTO> jobseekerJobDetailTOS = jobseekerJobDetailService.selectByJobseekerId(jobDb.getId());
            if (null == jobseekerJobDetailTOS || jobseekerJobDetailTOS.size() <= 0) {
                jobseekerJobDetailService.insert(jobDetails, job);
            } else {
                List<JobseekerJobDetailTO> updateList = jobseekerJobDetailTOS.stream().map(a -> a.setIsDelete(YesNoEnum.YES)).collect(Collectors.toList());
                int update = jobseekerJobDetailService.updateList(updateList);
                log.info("jobseeker   ---- 批量修改工作信息" + update + ",--------------list" + updateList.size());
                if (update != updateList.size()) {
                    throw new DataException(DataException.Type.FAIL, "修改工作信息失败");
                }
                //新增
                jobseekerJobDetailService.insert(jobDetails, job);
            }
        }

        return jobDb;
    }

    /**
     * 填写工作属性
     *
     * @param jobseekerJob
     * @param jobseekerJobDetail
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月26日 上午11:41:19
     */
    @Transactional
    public void fillJobseekerJob(JobseekerJobTO jobseekerJob) throws Throwable {
        log.info("填写工作属性--jobseekerJob[{}]", JSON.toJSONString(jobseekerJob));
        if (jobseekerService.countByPrimary(jobseekerJob.getId()) == 0)
            throw new BusinessException(BusinessException.Type.DEFAULT, "账户不存在");
        if (jobseekerJobService.countByPrimary(jobseekerJob.getId()) == 0)
            throw new BusinessException(BusinessException.Type.DEFAULT, "工作信息不存在");

        jobseekerService.update(new JobseekerTO().setStatus(JobseekerTO.Status.TEST1).setId(jobseekerJob.getId()));
        jobseekerJob.setUpdatedBy(jobseekerJob.getId());
        log.info("AssessmentDomain.fillJobseekerJob保存工作属性,IsStays: " + jobseekerJob.getIsStays().getValue());
        jobseekerJobService.update(jobseekerJob);
    }

    /**
     * 面试考核
     *
     * @param jobseekerJob
     * @param jobseekerJobDetail
     * @throws Throwable
     * @author 高杰       //type ==TEST3  result=ELIMINATE
     * @time 2018年12月26日 上午11:42:09
     */
    @Transactional
    public void assessJobseeker(JobseekerAssessTO assess, long operator) throws Throwable {
        log.info("面试考核,assess[{}]", JSON.toJSONString(assess));
        JobseekerTO jobseekerDb = jobseekerService.selectByLogin(assess.getLogin());
        if (jobseekerDb == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "账户不存在");
        if (jobseekerJobService.countByPrimary(jobseekerDb.getId()) < 1)
            throw new BusinessException(BusinessException.Type.DEFAULT, "缺少工作信息");
        if (jobseekerSocialService.countByPrimary(jobseekerDb.getId()) < 1)
            throw new BusinessException(BusinessException.Type.DEFAULT, "缺少个人信息");

        if (jobseekerDb.getStatus() == JobseekerTO.Status.GRADUATION
                || jobseekerDb.getStatus() == JobseekerTO.Status.TEST1ELIMINATE
                || jobseekerDb.getStatus() == JobseekerTO.Status.TEST1RESERVE
                || jobseekerDb.getStatus() == JobseekerTO.Status.TEST2ELIMINATE
                || jobseekerDb.getStatus() == JobseekerTO.Status.TEST2RESERVE
                || jobseekerDb.getStatus() == JobseekerTO.Status.INIT)
            throw new BusinessException(BusinessException.Type.DEFAULT, "不在考核中，不能操作");

        if (jobseekerDb.getStatus() != JobseekerTO.Status.valueOf(assess.getType().toString()))
            throw new BusinessException(BusinessException.Type.DEFAULT, "考核步骤不正确");

        CraftsmanTraceTO craftsmanTrace = new CraftsmanTraceTO().setLogin(jobseekerDb.getLogin());
        if (jobseekerDb.getStatus() == JobseekerTO.Status.TEST1)
            craftsmanTrace.setType(CraftsmanTraceTO.Type.TEST1);
        else if (jobseekerDb.getStatus() == JobseekerTO.Status.TEST2)
            craftsmanTrace.setType(CraftsmanTraceTO.Type.TEST2);
        craftsmanTraceService.insert(craftsmanTrace);

        JobseekerTO jobseeker = new JobseekerTO().setId(jobseekerDb.getId());

        //储备现在没有了
        if (assess.getResult() == JobseekerAssessTO.Result.RESERVE) {
            if (assess.getType() == JobseekerAssessTO.Type.TEST1)
                jobseeker.setStatus(JobseekerTO.Status.TEST1RESERVE);
            else if (assess.getType() == JobseekerAssessTO.Type.TEST2)
                jobseeker.setStatus(JobseekerTO.Status.TEST2RESERVE);
            //淘汰
        } else if (assess.getResult() == JobseekerAssessTO.Result.ELIMINATE) {
            if (assess.getType() == JobseekerAssessTO.Type.TEST1)
                //初试淘汰
                jobseeker.setStatus(JobseekerTO.Status.TEST1ELIMINATE).setOutTime(new Date());
            else if (assess.getType() == JobseekerAssessTO.Type.TEST2) {
                //初试淘汰
                jobseeker.setStatus(JobseekerTO.Status.TEST2ELIMINATE).setOutTime(new Date());
            }
            //初试通过
        } else if (assess.getType() == JobseekerAssessTO.Type.TEST1)
            jobseeker.setStatus(JobseekerTO.Status.TEST2);
        else if (assess.getType() == JobseekerAssessTO.Type.TEST2) {
            //通过复试
            jobseeker.setStatus(JobseekerTO.Status.GRADUATION);

            CraftsmanTO craftsmanDb = craftsmanService.selectByLogin(jobseekerDb.getLogin());
            if (craftsmanDb == null)
                copyJobseekerToCraftsman(jobseekerDb, operator);
            else
                copyJobseekerToCraftsman(craftsmanDb, jobseekerDb, operator);
        }

        assess.setCreatedBy(operator);
        jobseekerAssessService.insert(assess);
        jobseeker.setUpdatedBy(operator);
        jobseekerService.update(jobseeker);
    }

    /**
     * 毕业考核
     *
     * @param jobseekerJob
     * @param jobseekerJobDetail
     * @throws Throwable
     * @author 高杰       //type ==TEST3  result=ELIMINATE
     * @time 2018年12月26日 上午11:42:09
     */
    @Transactional
    public void assessCraftsman(JobseekerAssessTO assess, long operator) throws Throwable {
        log.info("毕业考核,assess[{}]", JSON.toJSONString(assess));
        JobseekerTO jobseekerDb = jobseekerService.selectByLogin(assess.getLogin());
        if (jobseekerDb == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "账户不存在");
        if (jobseekerJobService.countByPrimary(jobseekerDb.getId()) < 1)
            throw new BusinessException(BusinessException.Type.DEFAULT, "缺少工作信息");
        if (jobseekerSocialService.countByPrimary(jobseekerDb.getId()) < 1)
            throw new BusinessException(BusinessException.Type.DEFAULT, "缺少个人信息");

        if (jobseekerDb.getStatus() != JobseekerTO.Status.GRADUATION)
            throw new BusinessException(BusinessException.Type.DEFAULT, "没有通过复试，不能毕业考核");

        CraftsmanTO craftsman = craftsmanService.selectByLogin(assess.getLogin());
        if (craftsman == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "该员工不存在");

        CraftsmanTraceTO craftsmanTrace = new CraftsmanTraceTO().setLogin(assess.getLogin()).setType(CraftsmanTraceTO.Type.TEST3);
        craftsmanTraceService.insert(craftsmanTrace);

        if (assess.getResult() == JobseekerAssessTO.Result.RESERVE)
            //储备现在没有了
            craftsman.setStatus(CraftsmanTO.Status.RESERVE);
        else if (assess.getResult() == JobseekerAssessTO.Result.ELIMINATE) {
            //如果是淘汰的
            craftsman.setLeaveStatus(craftsman.getStatus());
            craftsman.setStatus(CraftsmanTO.Status.LEAVE);
            CraftsmanJobTO jobTO = craftsmanJobService.selectByPrimary(craftsman.getId());
            jobTO.setLeaveTime(new Date());
            craftsmanJobService.update(jobTO);
            CraftsmanLeavingTO craftsmanLeaving = new CraftsmanLeavingTO().setIsDelete(YesNoEnum.NO).setCraftsmanId(craftsman.getId())
                    .setReason("毕业考核淘汰").setRemark("毕业考核淘汰").setType(LeavingTypeEnum.FIRED).setSalaryDate(new Date());
            craftsmanLeavingService.insert(craftsmanLeaving);
            //TODO 需要新增离职原因
        } else if (assess.getType() == JobseekerAssessTO.Type.TEST3) {
            //变为飞虎队
            craftsman.setStatus(CraftsmanTO.Status.FLOW).setRegisterStep(1);

            CraftsmanDutyTO craftsmanDuty = craftsmanDutyService.selectBySequence(1);
            if (craftsmanDuty == null)
                throw new BusinessException(BusinessException.Type.DEFAULT, "一线员工岗位信息不存在");
            if (craftsmanDuty.getSequence() == 1) {
                craftsmanJobService.update(new CraftsmanJobTO().setId(craftsman.getId()).setDutyId(craftsmanDuty.getId())
                        .setUpdatedBy(operator).setGradTime(new Date()));
            }
        }

        assess.setCreatedBy(operator);
        jobseekerAssessService.insert(assess);
        craftsman.setUpdatedBy(operator);
        craftsmanService.update(craftsman);
    }

    /**
     * 重新初试
     *
     * @param Jobseeker
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月27日 上午10:22:35
     */
    @Transactional
    public void reset(JobseekerTO Jobseeker, long operator) throws Throwable {
        log.info("重新初试 Jobseeker[{}]", Jobseeker);
        JobseekerTO jobseekerDb = jobseekerService.selectByPrimary(Jobseeker.getId());
        if (jobseekerDb == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "账户不存在");

        if (!(jobseekerDb.getStatus() == JobseekerTO.Status.TEST1ELIMINATE
                || jobseekerDb.getStatus() == JobseekerTO.Status.TEST1RESERVE
                || jobseekerDb.getStatus() == JobseekerTO.Status.TEST2ELIMINATE
                || jobseekerDb.getStatus() == JobseekerTO.Status.TEST2RESERVE))
            throw new BusinessException(BusinessException.Type.DEFAULT, "只有被淘汰或储备的学员，才能重新初试");

        JobseekerTO jobseeker = new JobseekerTO().setId(Jobseeker.getId()).setStatus(JobseekerTO.Status.TEST1)
                .setUpdatedBy(operator);
        jobseekerService.update(jobseeker);
    }

    /**
     * 求职者首次成为发型师
     * 学员
     *
     * @param jobseekerDb
     * @author 高杰
     * @time 2018年12月27日 上午11:54:13
     */
    private void copyJobseekerToCraftsman(JobseekerTO jobseekerDb, long operator) throws Throwable {
        CraftsmanTO craftsman = CraftsmanTO.build().copy(jobseekerDb).setCreatedBy(operator).setUpdatedBy(operator).setRegisterStep(1);
        craftsmanService.insert(craftsman);

        JobseekerJobTO jobseekerJob = jobseekerJobService.selectByPrimary(jobseekerDb.getId());
        if (jobseekerJob == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "求职者工作信息不存在");
        CraftsmanDutyTO craftsmanDuty = craftsmanDutyService.selectByFirstSequence();
        if (craftsmanDuty == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "一线员工岗位信息不存在");
        CraftsmanLevelTO craftsmanLevel = craftsmanLevelService.selectByFirstSequence();
        if (craftsmanLevel == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "一线员工岗位信息不存在");

        craftsmanJobService.insert(new CraftsmanJobTO().copy(jobseekerJob).setDutyId(craftsmanDuty.getId()).setLevelId(craftsmanLevel.getId())
                .setId(craftsman.getId()).setCode(craftsman.getCode()).setCreatedBy(operator).setUpdatedBy(operator).setIsDelete(YesNoEnum.NO)
                //新增学员入职日期
                .setInTime(new Date()));

        JobseekerSocialTO jobseekerSocial = jobseekerSocialService.selectByPrimary(jobseekerDb.getId());
        if (jobseekerSocial == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "求职者个人信息不存在");
        craftsmanSocialService.insert(new CraftsmanSocialTO().copy(jobseekerSocial).setId(craftsman.getId())
                .setCode(craftsman.getCode()).setCreatedBy(operator).setUpdatedBy(operator).setIsDelete(YesNoEnum.NO).setWechatName(jobseekerDb.getWechatName()));

        List<JobseekerJobDetailTO> jobseekerJobDetailList = jobseekerJobDetailService
                .selectByJobseekerId(jobseekerDb.getId());
        if (!CommonUtil.isEmpty(jobseekerJobDetailList)) {
            List<CraftsmanJobDetailTO> craftsmanJobDetailList = new ArrayList<>(jobseekerJobDetailList.size());
            for (Iterator<JobseekerJobDetailTO> iterator = jobseekerJobDetailList.iterator(); iterator.hasNext(); ) {
                JobseekerJobDetailTO jobseekerJobDetail = iterator.next();
                craftsmanJobDetailList.add(CraftsmanJobDetailTO.build().copy(jobseekerJobDetail).setCreatedBy(operator)
                        .setUpdatedBy(operator).setCraftsmanId(craftsman.getId()));
            }
            craftsmanJobDetailService.insert(craftsmanJobDetailList);
        }
    }

    /**
     * 求职者非首次成为发型师
     *
     * @param jobseekerDb
     * @author 高杰
     * @time 2018年12月27日 上午11:54:13
     */
    private void copyJobseekerToCraftsman(CraftsmanTO craftsmanDb, JobseekerTO jobseekerDb, long operator)
            throws Throwable {
        craftsmanDb.reset(jobseekerDb.getType());
        craftsmanService.update(craftsmanDb.setUpdatedBy(operator));

        CraftsmanJobTO craftsmanJobDb = craftsmanJobService.selectByPrimary(craftsmanDb.getId());
        JobseekerJobTO jobseekerJob = jobseekerJobService.selectByPrimary(jobseekerDb.getId());
        if (craftsmanJobDb == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "求职者工作信息不存在");
        CraftsmanLevelTO craftsmanLevel = craftsmanLevelService.selectByFirstSequence();
        if (craftsmanLevel == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "职级不存在");
        CraftsmanDutyTO craftsmanDuty = craftsmanDutyService.selectByFirstSequence();
        if (craftsmanDuty == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "一线员工岗位信息不存在");

        craftsmanJobDb.setLevelId(craftsmanLevel.getId()).setDutyId(craftsmanDuty.getId());
        craftsmanJobService.update(craftsmanJobDb.copy(jobseekerJob.setUpdatedBy(operator)));

        CraftsmanSocialTO craftsmanSocialDb = craftsmanSocialService.selectByPrimary(craftsmanDb.getId());
        JobseekerSocialTO jobseekerSocial = jobseekerSocialService.selectByPrimary(jobseekerDb.getId());
        if (craftsmanSocialDb == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "求职者个人信息不存在");
        craftsmanSocialService.update(craftsmanSocialDb.copy(jobseekerSocial).setUpdatedBy(operator));

        List<JobseekerJobDetailTO> jobseekerJobDetailList = jobseekerJobDetailService
                .selectByJobseekerId(jobseekerDb.getId());
        if (!CommonUtil.isEmpty(jobseekerJobDetailList)) {
            List<CraftsmanJobDetailTO> craftsmanJobDetailList = new ArrayList<>(jobseekerJobDetailList.size());
            for (Iterator<JobseekerJobDetailTO> iterator = jobseekerJobDetailList.iterator(); iterator.hasNext(); ) {
                JobseekerJobDetailTO jobseekerJobDetail = iterator.next();

                CraftsmanJobDetailTO craftsmanJobDetail = craftsmanJobDetailService.selectByCraftsmanIdAndTime(
                        craftsmanDb.getId(), jobseekerJobDetail.getStartTime(), jobseekerJobDetail.getEndTime());
                if (craftsmanJobDetail != null)
                    craftsmanJobDetailService
                            .update(craftsmanJobDetail.copy(jobseekerJobDetail.setUpdatedBy(operator)));
                else
                    craftsmanJobDetailList.add(CraftsmanJobDetailTO.build()
                            .copy(jobseekerJobDetail.setUpdatedBy(operator).setCreatedBy(operator))
                            .setCraftsmanId(craftsmanDb.getId()));
            }

            if (craftsmanJobDetailList.size() > 0)
                craftsmanJobDetailService.insert(craftsmanJobDetailList);
        }
    }
}
